package com.nuanshui.heatedloan.socket.server.thread;


import java.io.ByteArrayInputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nuanshui.heatedloan.socket.ProtoMsg;
import com.nuanshui.heatedloan.socket.ProtoMsg.Msg;
import com.nuanshui.heatedloan.socket.msg.ReciPackBean;
import com.nuanshui.heatedloan.socket.msg.handler.HandlerFactory;
import com.nuanshui.heatedloan.socket.msg.handler.IHandler;

/**
 * 
 * 消息处理的线程
 * @author hulxg
 *
 */
public class ParseMsgThread extends Thread {

	private static final Logger logger = LoggerFactory.getLogger(ParseMsgThread.class);

	private ReciPackBean rpb;

	public ParseMsgThread(ReciPackBean rpb) {
		this.rpb = rpb;
	}

	@Override
	public void run() {
		try {
			
			// 接收到流并读取，如网络输入流，这里用ByteArrayInputStream来代替
			ByteArrayInputStream input = new ByteArrayInputStream(rpb.getMsgbytes());
		    for(int i = 0;i<rpb.getMsgbytes().length;i++){
		    	System.out.print(rpb.getMsgbytes()[i]+" ");
		    }
			System.out.println();
			// 反序列化		
			Msg msg =   ProtoMsg.Msg.parseFrom(rpb.getMsgbytes());	
			//调用消息处理
			IHandler handler =   HandlerFactory.getHandler(msg);
			handler.doHandle(msg, rpb.getChannel());
		} catch (Exception e) {
			logger.error("接受消息队列处理数据错误", e);
		}
	}
}
